let Draw=agcim.interactive.draw;
let agMath=agcim.maths.math;
let coordinate=agcim.maths.coordinate;
let AgLabel=agcim.geometry.Label;
let AgBillboard=agcim.geometry.Billboard;
let AgFeatureLayer=agcim.layer.FeatureLayer;

import picture from "../img/background.png";
var agFeatureLayer;
/**
 * Class AreaMeasure
 */
class AreaMeasure {
  constructor() {
    this.viewer = null;
    this.draw = null;
    agFeatureLayer = new AgFeatureLayer(CIM.viewer);
  }
  /**
   * @description: 初始化
   * @param {*} viewer 
   * @param {*} vm 
   */
  initialize(viewer, vm,polygon_pickElement = true) {
    var _this = this;
    _this.viewer = viewer;
    _this.draw = new Draw(viewer);
    _this.draw.drawOption.polygon_adjustToInSamePlane = true;
    _this.draw.drawOption.polygon_adjustHeight = 0.1;
    this.draw.drawOption.polygon_pickElement = polygon_pickElement;

    _this.draw.drawPolygon({ perPositionHeight: true }).then(result => {
      var positions = result.positions;
      //var positions2 = _this.updateMeasuerPoints(positions);

      // var area = agMath.getArea(positions);
      var area = 0
      if(positions.length > 2){
        area = agMath.getArea2(positions);
      }
      //var area3 = agMath.getArea2(positions2);

      // if (Math.abs(area * 1000 * 1000 - area2 * 1000 * 1000) > 10) {
      //   console.log("两个算面积的结果不一致，分别为" + area + "  // " + area2);
      // }
      var textArea = area < 1 ? (area == 0 ? 0 + "m²" : (area * 1000 * 1000).toFixed(5) + "m²") : area.toFixed(5) + "km²";
      vm.area = textArea;
      var lengths = 0;
      for (let i = 0; i < positions.length - 1; i++) {
        var lengthss = Math.sqrt(Math.pow((positions[i].x - positions[i + 1].x), 2) + Math.pow((positions[i].y - positions[i + 1].y), 2) + Math.pow((positions[i].z - positions[i + 1].z), 2));
        var lengths = lengthss + lengths;
      }
      var lent = Math.sqrt(Math.pow((positions[0].x - positions[positions.length - 1].x), 2) + Math.pow((positions[0].y - positions[positions.length - 1].y), 2) + Math.pow((positions[0].z - positions[positions.length - 1].z), 2));
      var distances = lengths + lent;
      var distance = positions.length < 2 ? 0 + "m" : distances.toFixed(2) + "m"
      vm.lengths = distance;

      var polyCenter = _this._getPolygonCenter(result.entity);
      var polygonHeight = _this._getLableHeight(positions);
      var labPosition = Cesium.Cartesian3.fromRadians(
        polyCenter.longitude,
        polyCenter.latitude,
        polygonHeight
      );
      var labelOpt = {
        font: "16px Helvetica",
        color: Cesium.Color.fromCssColorString('#fff'),
        pixelOffset: new Cesium.Cartesian2(0, -12),
        backgroundColor: Cesium.Color.fromCssColorString("#ce8f00"),
        style: Cesium.LabelStyle.FILL
      };
      var label = new AgLabel("agLabel", labPosition, textArea, labelOpt);
      label.addToLayer(agFeatureLayer);
      var labelOpt2 = {
        font: "16px Helvetica",
        color: Cesium.Color.fromCssColorString('#fff'),
        pixelOffset: new Cesium.Cartesian2(0, -40),
        backgroundColor: Cesium.Color.fromCssColorString("#ff5656"),
        style: Cesium.LabelStyle.FILL
      };
      var label2 = new AgLabel("agLabel", labPosition, distance, labelOpt2);
      label2.addToLayer(agFeatureLayer);
      // var billboardOpt = {
      //   width: 85,
      //   height: 25,
      // };
      // var billboard = new AgBillboard("agBillboard", labPosition, picture, billboardOpt);
      // billboard.addToLayer(agFeatureLayer);

    }, error => {
      console.log(error);
    });
  }

  /**
   * @description: 更新测量点
   * @param {*} points 
   * @returns 
   */
  updateMeasuerPoints(points) {
    var localCoordinate = agcim.maths.math.createLocalCoordinateHelper(points[0]);
    var points2 = localCoordinate.transformToLocal(points);

    var plane = agcim.maths.math.getPlaneByLSM(points2);
    var planePoints = agcim.maths.math.projectPointsAtPlane(points, points[0], plane);
    return planePoints;
  }
 
 /**
  * @description:获得标签的高度
  * @param {*} positions 
  * @returns 
  */
  _getLableHeight(positions) {
    var tempPoints = [];

    for (let i = 0; i < positions.length; i++) {
      var cartographic = coordinate.cartesian3ToCartographic(positions[i], "Degrees");
      tempPoints.push(cartographic);
    }
    var polygonHeight = tempPoints[0].alt;
    return polygonHeight;
  }
  /**
   * @description: 获取多边形中心
   * @param {*} entity 
   * @returns 
   */
  _getPolygonCenter(entity) {
    var polyPositions = entity.polygon.hierarchy.getValue(
      Cesium.JulianDate.now()
    ).positions;
    var polyCenter = Cesium.BoundingSphere.fromPoints(polyPositions).center;
    polyCenter = Cesium.Ellipsoid.WGS84.scaleToGeodeticSurface(polyCenter);
    polyCenter = Cesium.Cartographic.fromCartesian(polyCenter);
    return polyCenter;
  }
  /**
   * @description:销毁
   */
  dispose() {
    agFeatureLayer.removeAll();
    this.draw.dispose();
  }
}

export default new AreaMeasure();
